﻿using CarlifeService;
using Commonality.DTO.AuthorityDTO;
using ICarlifeRespository;
using ICarlifeService;
using Models.Authority;

namespace CarlifeService
{
    public class UsersService : BaseServer<Users>, IUsersService
    {
        private readonly IUsersRepository db;
        private readonly ISeconRepository secon;
        private readonly IUOfWork uOfWork;
        public UsersService(IUsersRepository db, ISeconRepository secon, IUOfWork uOfWork) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
            this.secon = secon;
        }

        public List<UserInfoDTO> GetUserAll(string? name = "", string? realname = "")
        {
            var list = from x in db.GetAll()
                       join y in secon.GetAll() on x.DropID equals y.Id
                       select new UserInfoDTO
                       {
                           UsleID = x.Id,
                           Login_Name = x.Login_Name,
                           UserName = x.UserName,
                           Password = x.Password,
                           RealName = x.RealName,
                           EnglishName = x.EnglishName,
                           Gender = x.Gender,
                           Phone = x.Phone,
                           Mobile = x.Mobile,
                           Email = x.Email,
                           Enabled = x.Enabled,
                           IsDelet = x.IsData,
                           Type = (State?)x.Type,
                           DropID = y.Id,
                           DropName = y.DropName,
                           PID = y.PID,
                       };
            list = list.Where(x => x.IsDelet == true);
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.UserName.Contains(name));
            }
            if (!string.IsNullOrEmpty(realname))
            {
                list = list.Where(x => x.RealName.Contains(realname));
            }
            return list.ToList();
        }

        public int GetUserAdd(Users entity)
        {
            db.Add(entity);
            return uOfWork.SaveChanges();
        }

        public Users GetUserByid(int id)
        {
            return db.GetById(id);
        }

        public int GetUserUpdate(Users entity)
        {
            db.Update(entity);
            return uOfWork.SaveChanges();
        }

        public int GetUserRemove(int id)
        {
            var list = db.GetById(id);
            list.IsData = false;
            db.Update(list);
            return uOfWork.SaveChanges();
        }
    }
}
